﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EmployeeCheckIn
{
    public partial class Check_timeEmp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateTime dateNow = DateTime.Now;
                DataTable dt  = GetData();
                string Time_Start_Data = dt.Rows[0]["TIME_OF_START"].ToString();
                TextBox_startJob.Text = dateNow.ToString(" dd MMMM yyyy ", new System.Globalization.CultureInfo("en-US")) + Time_Start_Data;
                TextBox_ID.Text = "";
                TextBox_Go.Text = "";
                TextBox_checkout.Text = "";             
            }
           
        }
      
        public string total(string s)
        {
            int HH;
            int mm;
            int ss;

            string[] stemp = s.Split(':');
            HH = Convert.ToInt32(stemp[0]);
            mm = Convert.ToInt32(stemp[1]);
            ss = Convert.ToInt32(stemp[2]);

            string overtime_str = ((HH * 60) + (mm) + (ss / 60)).ToString();
            return overtime_str;
        }
        
        protected void Button_click_Click(object sender, EventArgs e)
        {
            EmpEntity Entity = new EmpEntity();
            if(TextBox_checkout.Text=="")
            {
            string date1 = Convert.ToDateTime(TextBox_startJob.Text).ToString(" HH:mm ", new System.Globalization.CultureInfo("en-US"));
            string [] stemp1 = date1.Split(':');
            int HH = Convert.ToInt32(stemp1[0]);
            int mm = Convert.ToInt32(stemp1[1]);

            string date2 = Convert.ToDateTime(TextBox_Go.Text).ToString(" HH:mm ", new System.Globalization.CultureInfo("en-US"));
            string [] stemp2 = date2.Split(':');
            int HH1 = Convert.ToInt32(stemp2[0]);
            int mm1 = Convert.ToInt32(stemp2[1]);

            if (HH1 < HH)
            {
                Entity.ID = Convert.ToInt32(TextBox_ID.Text);
                Entity.CheckIN = TextBox_Go.Text;
                Entity.Late_time = 0;
            }
            if (HH1 > HH)
            {
                string count = (Convert.ToDateTime(TextBox_Go.Text) - Convert.ToDateTime(TextBox_startJob.Text)).ToString();
                Entity.ID = Convert.ToInt32(TextBox_ID.Text);
                Entity.CheckIN = TextBox_Go.Text;
                Entity.Late_time = 0;
            }
            if (HH1 == HH)
            {
                if (mm1 < mm)
                {
                Entity.ID = Convert.ToInt32(TextBox_ID.Text);
                Entity.CheckIN = TextBox_Go.Text;
                Entity.Late_time = 0;
                }
                if (mm1 > mm)
                {
                string count = (Convert.ToDateTime(TextBox_Go.Text) - Convert.ToDateTime(TextBox_startJob.Text)).ToString();
                Entity.ID = Convert.ToInt32(TextBox_ID.Text);
                Entity.CheckIN = TextBox_Go.Text;
                Entity.Late_time = Convert.ToInt32(total(count));
                }
                if (mm1 == mm)
                {
                    Entity.ID = Convert.ToInt32(TextBox_ID.Text);
                    Entity.CheckIN = TextBox_Go.Text;
                    Entity.Late_time = 0;
                }

            }
                
            InsertRecord(Entity);
            }//if check insert update;
            else
            {
                Entity.ID = Convert.ToInt32(TextBox_ID.Text);
                Entity.CheckIN = TextBox_Go.Text;
                Entity.CheckOut = TextBox_checkout.Text;
                UpdateRecord(Entity,Entity.ID,Entity.CheckIN);    
            }
        }
        private void InsertRecord(EmpEntity Entity)
        {
            string sqltext = @"insert into TBL_CHECK_IN (EMP_ID,[CHECK_IN],LATE_TIME)
                          
                           values (@EMP_ID,@CHECK_IN,@LATE_TIME)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection("Server=202.44.52.248;Database=DB_Personal;User Id=personal;Password=1234;");
                                                    
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@EMP_ID", Entity.ID);
            sqlcomm.Parameters.AddWithValue("@CHECK_IN", Entity.CheckIN);
            sqlcomm.Parameters.AddWithValue("@LATE_TIME", Entity.Late_time);
          
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        private DataTable GetData()
        {
            string sqltext = "SELECT TIME_OF_START from TBL_TIMEOFDAY ";
            SqlConnection sqlconn = new SqlConnection("Server=202.44.52.248;Database=DB_Personal;User Id=personal;Password=1234;");
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];

        }
        private void UpdateRecord(EmpEntity Insertentity, int i,string s)
        {

            string sqltext = @"UPDATE TBL_CHECK_IN SET 
                             CHECK_OUT= @Check_out 
                             where EMP_ID=(@EMP_ID) and CHECK_IN=(@CHECK_IN)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection("Server=202.44.52.248;Database=DB_Personal;User Id=personal;Password=1234;");
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@EMP_ID", i);
            sqlcomm.Parameters.AddWithValue("@CHECK_IN", s);
            sqlcomm.Parameters.AddWithValue("@Check_out", Insertentity.CheckOut);
                      
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        }
    }